﻿using System;
using System.Collections.ObjectModel;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Input;
using Acr.UserDialogs;
using Cashbook.Models;
using Cashbook.Services;
using Cashbook.Views;
using Xamarin.Forms;

namespace Cashbook.ViewModels
{
    public class EditBillVM: AbstractViewModel
    {
		private BillsDataAccess _billsService;
		private CustomersDataAccess _customerService;

		public ObservableRangeCollection<Customer> Customers { get; set; }
		//public Customer SelectedCustomer { get; set; }

		private INavigation _navigation;

		public Command LoadBillCommand { get; set; }
		public ICommand SaveCommand { get; private set; }
       // public ICommand IndexChangedCommand { get; private set; }
		protected IUserDialogs _dalogs { get; }

	

		Customer _selectedCustomer;
		public Customer SelectedCustomer
		{
			get { return this._selectedCustomer; }
			set
			{
				if (this._selectedCustomer == value)
					return;

				this._selectedCustomer = value;
				this.OnPropertyChanged();
			}
		}

		Bill _bill;
		public Bill Bill
		{
			get { return this._bill; }
			set
			{
				if (this._bill == value)
					return;

				this._bill = value;
				this.OnPropertyChanged();
			}
		}

		string _title;
		public string Title
		{
			get { return this._title; }
			set
			{
				if (this._title == value)
					return;

				this._title = value;
				this.OnPropertyChanged();
			}
		}

		int _billId;
		public int BillId
		{
			get { return this._billId; }
			set
			{
				if (this._billId == value)
					return;

				this._billId = value;
				this.OnPropertyChanged();
			}
		}



		public EditBillVM(int billId, INavigation navigation, IUserDialogs dialogs) : base(dialogs)
        {
			_navigation = navigation;
            BillId = billId;
			Title = BillId > 0 ? "编辑帐单" : "添加帐单";           

			_billsService = new BillsDataAccess();
            _customerService = new CustomersDataAccess();
            Customers = new ObservableRangeCollection<Customer>();
            Bill = new Bill{
                AddedDate = DateTime.Now,
                Amount = 0
            };

            LoadBillCommand = new Command(async () => await ExecuteLoadBillCommand());         
			SaveCommand = new Command(async () => await ExecuteSaveCommand());
		}

 

        async Task ExecuteLoadBillCommand()
        {
      
			try
			{
				Customers.Clear();
                var items = _customerService.GetCustomers();
				Customers.ReplaceRange(items);

			}
			catch (Exception ex)
			{
				// Debug.WriteLine(ex);
				await this.Dialogs.AlertAsync("帐单加载失败." + ex.Message, "错误", "确定");
			}
		
            if(BillId > 0){
				Bill = await _billsService.GetBillAsync(BillId);

				if (Bill.CustomerId > 0)
					SelectedCustomer = Customers.FirstOrDefault(c => c.Id == Bill.CustomerId);
            }
                
        }
		async Task ExecuteSaveCommand()
		{
            if (string.IsNullOrEmpty(Bill.Title))
			{
				
				await this.Dialogs.AlertAsync("请输入项目主题", "错误", "关闭");				
				return;
			}

			
            if (Bill.Amount <= 0)
			{
                await this.Dialogs.AlertAsync("请输入金额必须大于0", "错误", "关闭");				
				return;
			}

            if (Bill.CustomerId<=0)
			{
                await this.Dialogs.AlertAsync("请选择客户", "错误", "关闭");
			    return;
			}


			await _billsService.SaveBillAsync(Bill);

            		

			if (Bill.IsPayed)
				await _navigation.PushAsync(new BillIsPayedPage());
			else
			{
				await _navigation.PushAsync(new BillPage());
			}
		}
    }
}
